import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 30318
 * Date: 2023-05-15
 * Time: 20:45
 */
public class Test2 {

    public static void bubbleSort(int[] array) {
        //i控制趟数
        for (int i = 0; i < array.length-1; i++) {
            //优化：检查某一趟之后是否有序
            boolean flag = false;
            //array.length-1-i 优化的是 每一趟比较的次数
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j] > array[j+1]){
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    flag = true;
                }
            }
            if(flag == false){
                return;
            }
        }
    }

    public static void main(String[] args) {
        int[] array = {12, 6, 2, 8, 3, 9, 10};
        bubbleSort(array);
        System.out.println(Arrays.toString(array));
    }

    public static void reverse(int[] array){
        int left = 0;
        int right = array.length-1;
        while(left < right){
            int tmp = array[right];
            array[right] = array[left];
            array[left] = tmp;
            left++;
            right--;
        }
    }

    public static void main10(String[] args) {
        int[] array1 = new int[10];

        Arrays.fill(array1, 2, 5,-1);

        System.out.println(Arrays.toString(array1));


        /*//int[] array2 = {1, 3, 5, 7, 9};
        int[] array2 = array1;
        boolean flag = Arrays.equals(array1, array2);
        System.out.println(flag);*/

        /*reverse(array);
        System.out.println(Arrays.toString(array));*/
    }

    public static double avg(int[] array) {
        int sum = 0;
        for (int x:
             array) {
            sum += x;
        }
        return sum*1.0 / array.length;
    }

    public static int find(int[] array, int key){
        for (int i = 0; i < array.length; i++) {
            if(array[i] == key){
                return i;
            }
        }
        return -1;
    }

    //建立在有序的情况下
    public static int binarySearch(int[] array, int key){
        int left = 0;
        int right = array.length-1;
        while(left <= right){
            int mid = (left + right) >>> 1;
            if(array[mid] < key){
                left = mid+1;
            }else if(array[mid] > key){
                right = mid-1;
            }else{
                return mid;
            }
        }
        return -1;
    }

    public static void main9(String[] args) {
        int[] array = {1, 3, 5, 7, 9};
        //Arrays.sort(array);//不是冒泡排序 底层快排
        //降序需要给比较器

        System.out.println(Arrays.toString(array));

        //int index = binarySearch(array, 3);
        int index = Arrays.binarySearch(array, 3);

        System.out.println(index);

        //System.out.println(find(array, 9));
    }

    public static void main8(String[] args) {
        int[] array = {1, 3, 5, 7, 9};
        int[] array2 = array.clone();//产生一个副本
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(array2));

        array2[0] = 99;
        System.out.println("================");
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(array2));
    }

    public static void main7(String[] args) {
        int[] array = {1, 3, 5, 7, 9};

        //局部拷贝
        int[] array2 = Arrays.copyOfRange(array, 2, 5);//[2,5)

        System.out.println(Arrays.toString(array));

        System.out.println(Arrays.toString(array2));
    }

    public static void main6(String[] args) {
        int[] array = {1, 3, 5, 7, 9};
        int[] copy = new int[array.length];

        //支持局部拷贝
        System.arraycopy(array, 0, copy, 3, array.length-3);
        //5个参数：要拷贝的数组  拷贝的数组的开始位置 目的地数组 目的地数组的位置 长度

        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(copy));
    }

    public static void main5(String[] args) {
        int[] array = {1, 3, 5, 7, 9};

        //int[] array2 = Arrays.copyOf(array, array.length);
        //扩容 2倍
        int[] array2 = Arrays.copyOf(array, 2*array.length);

        System.out.println(Arrays.toString(array));

        System.out.println(Arrays.toString(array2));
    }

    public static void main4(String[] args) {
        int[] array = {1, 3, 5, 7, 9};

        int[] array2 = new int[array.length];

        for (int i = 0; i < array.length; i++) {
            array2[i] = array[i];
        }

        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(array2));

    }

    public static void main3(String[] args) {
        int[] array = {1, 3, 5, 7, 9};

        int[] array2 = array;
    }

    public static String myToString(int[] tmp){
        //assert tmp != null;//java里面用断言需要设置一些参数
        if(tmp == null){
            return null;
        }
        String ret = "[";
        for (int i = 0; i < tmp.length; i++) {
            ret += tmp[i];
            if(i == tmp.length-1){
                ret += ",";
            }
        }
        ret += "]";
        return ret;
    }

    public static void main2(String[] args) {
        int[] array = {1, 2, 3, 4};
        //String ret = Arrays.toString(array);
        String ret = myToString(array);
        System.out.println(ret);
    }

    public static void func(int[] tmp1) {
        tmp1 = new int[10];
        //tmp1 = {1, 2, 3, 4, 5};
    }

    public static void func2(int[] tmp1) {
        tmp1[0] = 99;
    }

    public static void main1(String[] args) {
        int[] array = {1, 2, 3, 4};
        System.out.println(array.length);
        int[] array2 = array;

        int[] array3 = null;

        int[] array4 = new int[]{1, 2, 3, 4, 5};

        int[] array5;
        array5 = new int[]{1, 2, 3, 4, 5};
    }
}
